{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "#import pickle\n",
    "#import pyarrow\n",
    "#import matplotlib.pyplot as plt\n",
    "#import statsmodels.tsa.filters as smtf\n",
    "import statsmodels.api as sm\n",
    "#import statsmodels.formula.api as smf\n",
    "#import statsmodels.tsa.stattools as smtsa\n",
    "#import matplotlib.ticker as mtick\n",
    "#import scipy.stats as stats\n",
    "#from tabulate import tabulate\n",
    "from stargazer.stargazer import Stargazer, LineLocation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_uk = pd.read_excel('../data/main_VAR/UK_full.xlsx')\n",
    "data_us = pd.read_excel('../data/main_VAR/US_full.xlsx')\n",
    "#data=data.pivot_table(index='year', columns=['country'])\n",
    "\n",
    "data_uk_comsumption=pd.read_excel('../data/supplement/UK_consumption.xlsx')\n",
    "data_us_comsumption=pd.read_excel('../data/supplement/US_consumption.xlsx')\n",
    "\n",
    "\n",
    "data_uk_comsumption.loc[data_uk_comsumption['delta_c']!=data_uk_comsumption['delta_c'],'delta_c']=np.log(data_uk_comsumption.loc[data_uk_comsumption['delta_c']!=data_uk_comsumption['delta_c'],'delta_c'])\n",
    "\n",
    "\n",
    "data_uk_comsumption.loc[data_uk_comsumption['delta_c']!=data_uk_comsumption['delta_c'],'delta_c']=np.log(data_uk_comsumption.loc[data_uk_comsumption['delta_c']!=data_uk_comsumption['delta_c'],'Consumption']).diff()\n",
    "\n",
    "data_uk=data_uk.merge(data_uk_comsumption[['year','delta_c']],left_on='Year',right_on='year')\n",
    "\n",
    "#data_us_comsumption.loc[:,'delta c']=-data_us_comsumption.loc[:,'delta c']\n",
    "\n",
    "\n",
    "data_us=data_us.merge(data_us_comsumption[['year','delta c']],left_on='year',right_on='year')\n",
    "\n",
    "\n",
    "data_uk['deltalogtau']=np.log(data_uk['revtogdp']).diff()+data_uk['real gdp growth']\n",
    "data_uk['deltalogg']=np.log(data_uk['spendingtogdp']).diff()+data_uk['real gdp growth']\n",
    "data_uk['deltas']=(data_uk['revtogdp']-data_uk['spendingtogdp']).diff()\n",
    "data_uk['real consumption growth']=data_uk['delta_c']\n",
    "\n",
    "\n",
    "data_us['deltalogtau']=np.log(data_us['tau']).diff()+data_us['x']\n",
    "data_us['deltalogg']=np.log(data_us['g']).diff()+data_us['x']\n",
    "data_us['deltas']=(data_us['tau']-data_us['g']).diff()\n",
    "#data_us['real gdp growth']=data_us['x']\n",
    "data_us['real consumption growth']=data_us['delta c']\n",
    "\n",
    "\n",
    "data_uk_1=data_uk.loc[(data_uk['Year']>=1830) & (data_uk['Year']<=1914) ]\n",
    "data_uk_2=data_uk.loc[(data_uk['Year']>=1947) & (data_uk['Year']<=2020) ]\n",
    "data_uk_3=data_uk.loc[(data_uk['Year']>=1830) & (data_uk['Year']<=1946) ]\n",
    "data_us_1=data_us.loc[(data_us['year']>=1830) & (data_us['year']<=1914) ]\n",
    "data_us_2=data_us.loc[(data_us['year']>=1947) & (data_us['year']<=2020) ]\n",
    "data_us_3=data_us.loc[(data_us['year']>=1830) & (data_us['year']<=1946) ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "reg_uk_1_tau=sm.OLS(endog=data_uk_1['deltalogtau'], exog=sm.add_constant(data_uk_1['real consumption growth']),missing = 'drop').fit()\n",
    "reg_uk_1_g=sm.OLS(endog=data_uk_1['deltalogg'], exog=sm.add_constant(data_uk_1['real consumption growth']),missing = 'drop').fit()\n",
    "reg_uk_1_s=sm.OLS(endog=data_uk_1['deltas'], exog=sm.add_constant(data_uk_1['real consumption growth']),missing = 'drop').fit()\n",
    "\n",
    "reg_uk_2_tau=sm.OLS(endog=data_uk_2['deltalogtau'], exog=sm.add_constant(data_uk_2['real consumption growth']),missing = 'drop').fit()\n",
    "reg_uk_2_g=sm.OLS(endog=data_uk_2['deltalogg'], exog=sm.add_constant(data_uk_2['real consumption growth']),missing = 'drop').fit()\n",
    "reg_uk_2_s=sm.OLS(endog=data_uk_2['deltas'], exog=sm.add_constant(data_uk_2['real consumption growth']),missing = 'drop').fit()\n",
    "\n",
    "reg_us_1_tau=sm.OLS(endog=data_us_1['deltalogtau'], exog=sm.add_constant(data_us_1['real consumption growth']),missing = 'drop').fit()\n",
    "reg_us_1_g=sm.OLS(endog=data_us_1['deltalogg'], exog=sm.add_constant(data_us_1['real consumption growth']),missing = 'drop').fit()\n",
    "reg_us_1_s=sm.OLS(endog=data_us_1['deltas'], exog=sm.add_constant(data_us_1['real consumption growth']),missing = 'drop').fit()\n",
    "\n",
    "reg_us_2_tau=sm.OLS(endog=data_us_2['deltalogtau'], exog=sm.add_constant(data_us_2['real consumption growth']),missing = 'drop').fit()\n",
    "reg_us_2_g=sm.OLS(endog=data_us_2['deltalogg'], exog=sm.add_constant(data_us_2['real consumption growth']),missing = 'drop').fit()\n",
    "reg_us_2_s=sm.OLS(endog=data_us_2['deltas'], exog=sm.add_constant(data_us_2['real consumption growth']),missing = 'drop').fit()\n",
    "\n",
    "reg_uk_3_tau=sm.OLS(endog=data_uk_3['deltalogtau'], exog=sm.add_constant(data_uk_3['real consumption growth']),missing = 'drop').fit()\n",
    "reg_uk_3_g=sm.OLS(endog=data_uk_3['deltalogg'], exog=sm.add_constant(data_uk_3['real consumption growth']),missing = 'drop').fit()\n",
    "reg_uk_3_s=sm.OLS(endog=data_uk_3['deltas'], exog=sm.add_constant(data_uk_3['real consumption growth']),missing = 'drop').fit()\n",
    "\n",
    "reg_us_3_tau=sm.OLS(endog=data_us_3['deltalogtau'], exog=sm.add_constant(data_us_3['real consumption growth']),missing = 'drop').fit()\n",
    "reg_us_3_g=sm.OLS(endog=data_us_3['deltalogg'], exog=sm.add_constant(data_us_3['real consumption growth']),missing = 'drop').fit()\n",
    "reg_us_3_s=sm.OLS(endog=data_us_3['deltas'], exog=sm.add_constant(data_us_3['real consumption growth']),missing = 'drop').fit()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "columns=['$Delta log T_us$', '$Delta log T_uk$', '$Delta log G_us$', '$Delta log G_uk$', '$Delta s_us$' , '$Delta s_uk$']\n",
    "\n",
    "stargazer1 = Stargazer([reg_us_1_tau, reg_uk_1_tau, reg_us_1_g, reg_uk_1_g, reg_us_1_s,reg_uk_1_s ])\n",
    "stargazer1.custom_columns(columns,[1,1,1,1,1,1])\n",
    "stargazer1.significant_digits(2)\n",
    "stargazer1.show_degrees_of_freedom(False)\n",
    "\n",
    "output1=stargazer1.render_latex()\n",
    "output1=output1.replace('Delta',\"\\Delta\")\n",
    "output1=output1.replace('log',\"\\log\")\n",
    "#output1=output1.replace('tau',\"T\")\n",
    "output1=output1.replace('_us',\"_{us}\")\n",
    "output1=output1.replace('_uk',\"_{uk}\")\n",
    "\n",
    "f = open(\"../results/tables_consumption_cyclicality/cyc_table_sample1_consumption.tex\", \"w\")\n",
    "f.write(output1)\n",
    "f.close()\n",
    "\n",
    "stargazer2 = Stargazer([reg_us_2_tau, reg_uk_2_tau, reg_us_2_g, reg_uk_2_g, reg_us_2_s,reg_uk_2_s ])\n",
    "stargazer2.custom_columns(columns,[1,1,1,1,1,1])\n",
    "stargazer2.significant_digits(2)\n",
    "stargazer2.show_degrees_of_freedom(False)\n",
    "\n",
    "output2=stargazer2.render_latex()\n",
    "output2=output2.replace('Delta',\"\\Delta\")\n",
    "output2=output2.replace('log',\"\\log\")\n",
    "#output2=output2.replace('tau',\"\\\\tau\")\n",
    "output2=output2.replace('_us',\"_{us}\")\n",
    "output2=output2.replace('_uk',\"_{uk}\")\n",
    "\n",
    "f = open(\"../results/tables_consumption_cyclicality/cyc_table_sample2_consumption.tex\", \"w\")\n",
    "f.write(output2)\n",
    "f.close()\n",
    "\n",
    "stargazer3 = Stargazer([reg_us_3_tau, reg_uk_3_tau, reg_us_3_g, reg_uk_3_g, reg_us_3_s,reg_uk_3_s ])\n",
    "stargazer3.custom_columns(columns,[1,1,1,1,1,1])\n",
    "stargazer3.significant_digits(2)\n",
    "stargazer3.show_degrees_of_freedom(False)\n",
    "\n",
    "output3=stargazer3.render_latex()\n",
    "output3=output3.replace('Delta',\"\\Delta\")\n",
    "output3=output3.replace('log',\"\\log\")\n",
    "#output3=output3.replace('tau',\"\\\\tau\")\n",
    "output3=output3.replace('_us',\"_{us}\")\n",
    "output3=output3.replace('_uk',\"_{uk}\")\n",
    "\n",
    "f = open(\"../results/tables_consumption_cyclicality/cyc_table_sample3_consumption.tex\", \"w\")\n",
    "f.write(output3)\n",
    "f.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
